import { createStore } from 'vuex';
import { getToken, setToken } from '@/utils/auth';
import router from '@/router';

export default createStore({
  state: {
    token: getToken(),
  },
  actions: {
    logout({ commit }) {
      commit('SET_TOKEN', null);
      router.replace('/');
    },
  },
  mutations: {
    SET_TOKEN(state, token) {
      setToken(token);
      state.token = token;
    },
  },
});
